﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Log;
using mepostras.BLL;
using mepostras.BO.Util;
using mepostras.DAL;
using mepostras.Settings.BusinessLayer;
using GeneralCode = mepostras.BO.Util.BoConfig.MessageCode.General;
namespace mepostras.BO
{
    public class AppointmentBo : IAppointmentBo
    {
        /// <summary>
        /// Decode để hiển thị ra Html
        /// </summary>
        /// <param name="appointment"></param>
        /// <returns></returns>
        private void HtmlDecode(Appointment appointment)
        {
            if (appointment != null)
            {
                appointment.Doctor = HttpUtility.HtmlDecode(appointment.Doctor);
                appointment.Description = HttpUtility.HtmlDecode(appointment.Description);
                appointment.FirstName = HttpUtility.HtmlDecode(appointment.FirstName);
                appointment.LastName = HttpUtility.HtmlDecode(appointment.LastName);
            }
        }

        /// <summary>
        /// Encode để hiển thị ra Html
        /// </summary>
        /// <param name="appointment"></param>
        /// <returns></returns>
        private void HtmlEncode(Appointment appointment)
        {
            if (appointment != null)
            {
                appointment.Doctor = HttpUtility.HtmlEncode(appointment.Doctor);
                appointment.Description = HttpUtility.HtmlEncode(appointment.Description);
                appointment.FirstName = HttpUtility.HtmlEncode(appointment.FirstName);
                appointment.LastName = HttpUtility.HtmlEncode(appointment.LastName);
            }
        }

        public string RenderLocalId(int id)
        {
            return String.Format(BoConfig.Format.LocalId, BoConfig.Prefix.Appointment, id);
        }
        
        /// <summary>
        /// cập nhật appointment
        /// </summary>
        /// <param name="appointment"></param>
        /// <param name="messageCode"></param>
        /// <returns></returns>
        public bool Update(Appointment appointment, out string messageCode)
        {
            messageCode = GeneralCode.ServerError;
            bool result = false;
            try
            {
                HtmlEncode(appointment);

                // Nếu chưa có LocalId thì update nó luôn
                if (string.IsNullOrEmpty(appointment.LocalId))
                {
                    appointment.LocalId = RenderLocalId(appointment.Id);
                }

                // Lấy kết quả trả về
                result = DataRepository.AppointmentProvider.Update(appointment);

                // Dựa vào kết quả trả về mà set mã lỗi
                messageCode = result ? GeneralCode.UpdateSuccess : GeneralCode.UpdateFail;
            }
            catch (Exception ex)
            {
                SingletonLogger.Instance.Error("Update", ex);
            }
            return result;
        }

        public bool Insert(Appointment appointment, out string messageCode)
        {
            messageCode = GeneralCode.ServerError;
            bool result = false;
            try
            {
                appointment.CreatedDate = DateTime.Now.Date;

                HtmlEncode(appointment);

                // Lấy kết quả trả về
                result = DataRepository.AppointmentProvider.Insert(appointment);

                // Nếu thành công thì cập nhật LocalId
                if (result)
                {
                    appointment.LocalId = RenderLocalId(appointment.Id);
                    DataRepository.AppointmentProvider.Update(appointment);
                }

                // Dựa vào kết quả trả về mà set mã lỗi
                messageCode = result ? GeneralCode.InsertSuccess : GeneralCode.InsertFail;
            }
            catch (Exception ex)
            {
                SingletonLogger.Instance.Error("Insert", ex);
            }
            return result;
        }

        public bool Book(Appointment appointment, out string messageCode)
        {
            // Kiểm tra time xem hợp lệ ko
            var date = DateTime.Now;
            date = new DateTime(date.Year, date.Month, date.Day + 1);
            if (!appointment.FromTime.HasValue || appointment.FromTime.Value < date)
            {
                messageCode = BoConfig.MessageCode.Appointment.InvalidAppointmentTime;
                return false;
            }

            if (Insert(appointment, out messageCode))
            {
                messageCode = BoConfig.MessageCode.Appointment.BookAppointmentSuccess;
                return true;
            }

            messageCode = BoConfig.MessageCode.Appointment.BookAppointmentFail;
            return false;
        }

        public bool Delete(int id, out string messageCode)
        {
            messageCode = GeneralCode.ServerError;
            bool result = false;
            try
            {
                result = DataRepository.AppointmentProvider.Delete(id);
                messageCode = result ? GeneralCode.DeleteSuccess : GeneralCode.DeleteFail;
            }
            catch (Exception ex)
            {
                SingletonLogger.Instance.Error("Delete", ex);
            }
            return result;
        }
    }
}
